package com.learning.platform.controller;

import com.learning.platform.dto.ApiResponse;
import com.learning.platform.security.UserPrincipal;
import com.learning.platform.service.ProgressService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/progress")
public class ProgressController {
    
    private final ProgressService progressService;
    
    public ProgressController(ProgressService progressService) {
        this.progressService = progressService;
    }
    
    @PostMapping
    public ApiResponse<Void> updateProgress(@RequestBody Map<String, Object> request, 
                                          Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        Long lessonId = Long.valueOf(request.get("lessonId").toString());
        Integer progressPercent = Integer.valueOf(request.get("progressPercent").toString());
        
        progressService.updateProgress(principal.getUserId(), lessonId, progressPercent);
        return ApiResponse.success(null, "进度更新成功");
    }
    
    @GetMapping
    public ApiResponse<Map<String, Integer>> getProgress(@RequestParam Long lessonId, 
                                                       Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        Integer progress = progressService.getProgress(principal.getUserId(), lessonId);
        return ApiResponse.success(Map.of("progressPercent", progress));
    }
}










